package com.amoyhong.hongWhether.model;

import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2016/1/9.
 */
public class WeatherData {

    private static SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd HH:mm");

    private String dateString;

    private Date date;

    private double temperature;

    private String description;

    private String IconUrl;

    private String locationName;

    private WeatherData() {
    }

    public WeatherData(Date date, String dateString, String description, String iconUrl, String locationName, double temperature) {
        this.date = date;
        this.dateString = dateString;
        this.description = description;
        IconUrl = iconUrl;
        this.locationName = locationName;
        this.temperature = temperature;
    }

    public static WeatherData newNoDateWeatherData(String description, String iconUrl, String locationName, double temperature) {
        WeatherData weatherData = new WeatherData();
        weatherData.description = description;
        weatherData.IconUrl = iconUrl;
        weatherData.locationName = locationName;
        weatherData.temperature = temperature;
        return weatherData;
    }

    public static WeatherData newNoLocationWeatherData(Date date, String dateString, String description, double temperature) {
        WeatherData weatherData = new WeatherData();
        weatherData.description = description;
        weatherData.date = new Date(date.getTime());
        weatherData.dateString = dateString;
        weatherData.temperature = temperature;
        return weatherData;
    }

    public static class SingleWeatherDataDeserializer implements JsonDeserializer<WeatherData> {

        @Override
        public WeatherData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

            final JsonObject jsonObject = json.getAsJsonObject();

            String locationName = jsonObject.get("name").getAsString();

            JsonArray whetherArray = jsonObject.getAsJsonArray("weather");
            JsonObject whetherInfo0 = (JsonObject) whetherArray.get(0);
            String description = whetherInfo0.get("description").getAsString();
            String IconUrl = whetherInfo0.get("icon").getAsString();

            JsonObject mainObject = jsonObject.getAsJsonObject("main");
            double temperature = mainObject.get("temp").getAsDouble();

            WeatherData weatherData = WeatherData.newNoDateWeatherData(description, IconUrl, locationName, temperature);

            return weatherData;

        }
    }

    public Date getDate() {
        return new Date(date.getTime());
    }

    public static SimpleDateFormat getDateFormat() {
        return dateFormat;
    }

    public String getDateString() {
        return dateString;
    }

    public String getDescription() {
        return description;
    }

    public String getIconUrl() {
        return IconUrl;
    }

    public String getLocationName() {
        return locationName;
    }

    public double getTemperature() {
        return temperature;
    }

    @Override
    public String toString() {
        return "WeatherData{" +
                "description='" + description + '\'' +
                ", temperature=" + temperature +
                '}';
    }
}
